<?php

namespace App\Http\Controllers\Client;

use App\Models\MemberModel;
use App\Services\MemberService;
use App\Utils\ApiResponse;
use App\Utils\JwtUtil;
use App\Utils\PriceUtis;
use Illuminate\Http\Request;

class MemberController extends ClientBase
{
    /**
     * 注册方法
     * @param Request $request
     */
    public function register(Request $request)
    {
        $data = $request->decrypted_data;
        $number = (string) $data->number;
        $passWord = (string) $data->password;
        $invitationCode = (string) $data->invitation_code;
        if (!$number) {
            ApiResponse::fail("用户名不能为空");
        }
        if (!$passWord) {
            ApiResponse::fail("密码不能为空");
        }
        $memberService = new MemberService();
        $memberInfo = $memberService->getMemberInfoByNumber($number);
        if ($memberInfo) {
            ApiResponse::fail("账号已存在");
        }
        $pwdHash = $memberService->passEncode($passWord);
        $newMember = new MemberModel();
        $newMember->number = $number;
        $newMember->password = $pwdHash;
        $newMember->invitation_code = random_str(20);
        if ($invitationCode != "") {
            $invitationMember = $memberService->getMemberInfoByInvitationCode($invitationCode);
            if (empty($invitationMember)) {
                ApiResponse::fail("邀请码不存在");
            }
        } else {
            $invitationMember = $memberService->getMemberInfoById(1);
        }
        $newMember->invitation_uid = $invitationMember->id;
        $newMember->save();
        $token = JwtUtil::getToken([
            'user_id'     => $newMember->id,
            'user_number' => $newMember->number,
        ]);
        ApiResponse::success([
            "user_id" => $newMember->id,
            "token"   => $token
        ]);
    }

    /**
     * 登录
     * @param Request $request
     */
    public function login(Request $request)
    {
        $data = $request->decrypted_data;
        $number = (string) $data->number;
        $passWord = (string) $data->password;
        if (!$number) {
            ApiResponse::fail("用户名不能为空");
        }
        if (!$passWord) {
            ApiResponse::fail("密码不能为空");
        }
        $memberService = new MemberService();
        $memberInfo = $memberService->getMemberInfoByNumber($number);
        if (!$memberInfo) {
            ApiResponse::fail("账号不存在");
        }
        if (!$memberService->passCheck($passWord, $memberInfo->password)) {
            ApiResponse::fail("密码不正确");
        }
        $token = JwtUtil::getToken([
            'user_id'     => $memberInfo->id,
            'user_number' => $memberInfo->number
        ]);
        ApiResponse::success([
            "token" => $token
        ]);
    }

    /**
     * 设置用户信息（包含openid）
     * @param Request $request
     */
    public function setMemberInfo(Request $request)
    {
        $data = $request->decrypted_data;
        $code = $data->code;
        if (!isset($data->member_info)) {
            ApiResponse::fail("用户信息不能为空");
        }
        $memberInfoArr = json_decode($data->member_info, true);
        if (empty($memberInfoArr)) {
            ApiResponse::fail("用户信息异常");
        }
        $memberService = new MemberService();
        [$status, $openId] = $memberService->getOpenid($code);
        if (!$status) {
            ApiResponse::fail("openid获取失败");
        }
        $this->currentUser->nick_name = $memberInfoArr['nick_name'] ?? "";
        $this->currentUser->gender = $memberInfoArr['gender'] ?? 0;
        $this->currentUser->avatar_url = $memberInfoArr['avatar_url'] ?? "";
        $this->currentUser->openid = $openId ?? "";
        $this->currentUser->auth_time = time();
        $res = $this->currentUser->save();
        $this->currentUser->auth_time = date("Y-m-d H:i");
        if ($res) {
            ApiResponse::success([
                'member_info' => $this->currentUser
            ]);
        } else {
            ApiResponse::fail();
        }
    }

    /**
     * 获取用户信息
     */
    public function getMemberInfo()
    {
        $this->currentUser->auth_time = date("Y-m-d H:i");
        $this->currentUser->red_pack = PriceUtis::handle0(PriceUtis::toYuan($this->currentUser->red_pack));
        $this->currentUser->boolean = PriceUtis::handle0(PriceUtis::toYuan($this->currentUser->boolean));
        ApiResponse::success([
            'member_info' => $this->currentUser
        ]);
    }

    /**
     * 用户的团队
     */
    public function team()
    {
        $memberService = new MemberService();
        $teamList = $memberService->getMemberListByInvitationUid($this->currentUser->id);
        ApiResponse::success([
            'member_list' => $teamList
        ]);
    }

}
